<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import { User, Lock, Key, Message } from '@element-plus/icons-vue'
import loginApi from '../api/login_api.js'

const phone = ref({
  phone: '',
  password: '',
  captcha: '',
  key: ''
});

//找回密码相关数据
const forgetForm = ref({
  email: '',
  code: ''
});

const isForgetPassword = ref(false);
const isSendingCode = ref(false);
const countdown = ref(0);
let timer = null;

onMounted(function(){
  localStorage.removeItem("token");
  // 加载验证码
  loadCaptcha();
})

onBeforeUnmount(() => {
  if (timer) {
    clearInterval(timer);
  }
})

// 获取路由对象
const router = useRouter();

// 验证码图片数据
const captcha = ref("");

// 绑定方法
const methods = {
  onSubmit: async () => {
    console.log("登录按钮被点击");
    const res = await loginApi.login(phone.value);
    if (res) {
      localStorage.setItem("token",res);
      //跳转
      await  router.push("/");
    }
  },

  // 加载验证码
  loadCaptcha: async () => {
    console.log("加载验证码");
    const res=await loginApi.captcha();
    phone.value.key=res.key;
    captcha.value=res.base;
  },

  // 显示找回密码表单
  showForgetPassword: () => {
    console.log("显示找回密码表单");
    isForgetPassword.value = true;
  },

  // 返回登录表单
  backToLogin: () => {
    console.log("返回登录表单");
    isForgetPassword.value = false;
    countdown.value = 0;
    if (timer) {
      clearInterval(timer);
      timer = null;
    }
  },

  // 发送验证码
  sendVerificationCode: async () => {
    console.log("发送验证码按钮被点击");
    if (!forgetForm.value.email) {
      ElMessage.warning('请输入邮箱地址');
      return;
    }

    // 简单的邮箱格式验证
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    if (!emailRegex.test(forgetForm.value.email)) {
      ElMessage.warning('请输入正确的邮箱格式');
      return;
    }

    // 调用后端API发送验证码
    isSendingCode.value = true;

    try {
      // 假设你有一个发送验证码的API接口
      const res = await loginApi.sendVerificationCode(forgetForm.value.email);
      if (res) {
        ElMessage.success('验证码已发送至您的邮箱，请注意查收');

        // 启动倒计时
        countdown.value = 60;
        if (timer) {
          clearInterval(timer);
        }
        timer = setInterval(() => {
          if (countdown.value > 0) {
            countdown.value--;
          } else {
            clearInterval(timer);
            timer = null;
          }
        }, 1000);
      }
    } catch (error) {
      ElMessage.error('发送验证码失败，请稍后重试');
    } finally {
      isSendingCode.value = false;
    }
  },

  // 提交找回密码表单
  submitForgetPassword: async () => {
    console.log("重置密码按钮被点击");
    if (!forgetForm.value.email || !forgetForm.value.code) {
      ElMessage.warning('请填写完整信息');
      return;
    }

    // 简单的验证码格式验证
    if (forgetForm.value.code.length !== 6) {
      ElMessage.warning('请输入6位验证码');
      return;
    }

    try {
      // 调用后端重置密码API
      const res = await loginApi.resetPassword({
        email: forgetForm.value.email,
        code: forgetForm.value.code
      });

      if (res) {
        ElMessageBox.alert('密码重置成功，新密码已发送至您的邮箱', '操作成功', {
          confirmButtonText: '确定',
          type: 'success',
        }).then(() => {
          methods.backToLogin();
        }).catch(() => {
          methods.backToLogin();
        });
      }
    } catch (error) {
      ElMessage.error('重置密码失败，请稍后重试');
    }
  }
}

// 解构methods中的方法，使其可以直接在模板中使用
const { onSubmit, loadCaptcha, showForgetPassword, backToLogin, sendVerificationCode, submitForgetPassword } = methods;
</script>

<template>
  <div class="cyberpunk-login-container">
    <!-- 背景网格 -->
    <div class="cyber-grid"></div>

    <!-- 霓虹灯效果 -->
    <div class="neon-circles">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
    </div>

    <div class="cyber-login-box">
      <!-- 登录表单 -->
      <el-form v-if="!isForgetPassword" class="cyber-form-login" :model="phone">
        <div class="cyber-logo-section">
          <div class="logo-wrapper">
            <div class="logo-glow"></div>
            <img class="cyber-logo" src="@/assets/logo.png" alt="中享思途" />
          </div>
          <h1 class="cyber-title">系统登录</h1>
        </div>
        <el-form-item>
          <div class="cyber-input-wrapper">
            <el-input v-model="phone.phone"
                      placeholder="请输入手机号"
                      :prefix-icon="User"
                      maxlength="16"
                      size="large"
                      class="cyber-custom-input"></el-input>
          </div>
        </el-form-item>
        <el-form-item>
          <div class="cyber-input-wrapper">
            <el-input v-model="phone.password"
                      placeholder="请输入密码"
                      show-password
                      :prefix-icon="Lock"
                      maxlength="16"
                      size="large"
                      class="cyber-custom-input"></el-input>
          </div>
        </el-form-item>
        <el-form-item class="cyber-captcha-section">
          <div class="cyber-input-wrapper">
            <el-input v-model="phone.captcha"
                      placeholder="验证码"
                      :prefix-icon="Key"
                      maxlength="5"
                      size="large"
                      class="cyber-custom-input cyber-captcha-input"></el-input>
          </div>
          <img :src="captcha"
               class="cyber-img-verCode" @click="loadCaptcha">
        </el-form-item>
        <el-form-item>
          <el-button class="cyber-btn-login" type="primary" size="large" @click="onSubmit">
            <span class="btn-text">登录</span>
            <div class="btn-glow"></div>
          </el-button>
        </el-form-item>
        <el-form-item class="cyber-forget-password">
          <el-button link type="primary" @click="showForgetPassword" class="cyber-link">忘记密码?</el-button>
        </el-form-item>
      </el-form>

      <!-- 找回密码表单 -->
      <el-form v-else class="cyber-form-login" :model="forgetForm">
        <div class="cyber-logo-section">
          <div class="logo-wrapper">
            <div class="logo-glow"></div>
            <img class="cyber-logo" src="@/assets/logo.png" alt="中享思途" />
          </div>
          <h1 class="cyber-title">找回密码</h1>
        </div>
        <el-form-item>
          <div class="cyber-input-wrapper">
            <el-input v-model="forgetForm.email"
                      placeholder="请输入注册邮箱"
                      :prefix-icon="Message"
                      maxlength="50"
                      size="large"
                      class="cyber-custom-input"></el-input>
          </div>
        </el-form-item>
        <el-form-item class="cyber-verification-section">
          <div class="cyber-input-wrapper">
            <el-input v-model="forgetForm.code"
                      placeholder="请输入验证码"
                      :prefix-icon="Key"
                      maxlength="6"
                      size="large"
                      class="cyber-custom-input cyber-verification-input"></el-input>
          </div>
          <el-button
              @click="sendVerificationCode"
              :disabled="isSendingCode || countdown > 0"
              type="primary"
              size="large"
              class="cyber-send-code-btn">
            <span class="btn-text">{{ countdown > 0 ? `${countdown}秒后重发` : (isSendingCode ? '发送中...' : '发送验证码') }}</span>
            <div class="btn-glow"></div>
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button class="cyber-btn-login" type="primary" size="large" @click="submitForgetPassword">
            <span class="btn-text">重置密码</span>
            <div class="btn-glow"></div>
          </el-button>
        </el-form-item>
        <el-form-item class="cyber-forget-password">
          <el-button link type="primary" @click="backToLogin" class="cyber-link">返回登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
.cyberpunk-login-container {
  height: 100vh;
  background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
  font-family: 'Courier New', monospace;
}

/* 网格背景 */
.cyber-grid {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image:
    linear-gradient(rgba(0, 243, 255, 0.1) 1px, transparent 1px),
    linear-gradient(90deg, rgba(0, 243, 255, 0.1) 1px, transparent 1px);
  background-size: 30px 30px;
  z-index: 0;
}

/* 霓虹圆环 */
.neon-circles {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.circle {
  position: absolute;
  border-radius: 50%;
  filter: blur(30px);
  opacity: 0.3;
}

.circle-1 {
  width: 300px;
  height: 300px;
  background: #00f3ff;
  top: 10%;
  left: 10%;
  animation: float 8s infinite ease-in-out;
}

.circle-2 {
  width: 200px;
  height: 200px;
  background: #ff00ff;
  bottom: 10%;
  right: 10%;
  animation: float 6s infinite ease-in-out reverse;
}

.circle-3 {
  width: 150px;
  height: 150px;
  background: #ffff00;
  top: 50%;
  left: 20%;
  animation: float 10s infinite ease-in-out;
}

.cyber-login-box {
  position: relative;
  z-index: 2;
  width: 100%;
  max-width: 420px;
  padding: 20px;
}

.cyber-form-login {
  background: rgba(20, 20, 40, 0.85);
  backdrop-filter: blur(12px);
  border-radius: 16px;
  padding: 40px 30px;
  box-shadow:
    0 0 20px rgba(0, 243, 255, 0.3),
    0 0 40px rgba(0, 243, 255, 0.1),
    inset 0 0 10px rgba(0, 243, 255, 0.2);
  border: 1px solid rgba(0, 243, 255, 0.3);
  position: relative;
  overflow: hidden;
}

.cyber-form-login::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: linear-gradient(90deg, transparent, #00f3ff, #ff00ff, transparent);
  animation: scanline 3s linear infinite;
}

.cyber-logo-section {
  text-align: center;
  margin-bottom: 30px;
  position: relative;
}

.logo-wrapper {
  position: relative;
  display: inline-block;
}

.logo-glow {
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(0, 243, 255, 0.5), transparent 70%);
  animation: pulse 2s infinite;
}

.cyber-logo {
  width: 90px;
  height: 90px;
  border-radius: 50%;
  object-fit: contain;
  background: rgba(10, 10, 25, 0.9);
  padding: 12px;
  box-shadow: 0 0 20px rgba(0, 243, 255, 0.5);
  border: 2px solid #00f3ff;
  position: relative;
  z-index: 2;
}

.cyber-title {
  text-align: center;
  font-size: 28px;
  margin-top: 20px;
  margin-bottom: 10px;
  color: #00f3ff;
  font-weight: 700;
  text-shadow: 0 0 10px rgba(0, 243, 255, 0.7);
  letter-spacing: 2px;
}

.cyber-input-wrapper {
  position: relative;
}

.cyber-input-wrapper::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border: 1px solid rgba(0, 243, 255, 0.5);
  border-radius: 8px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.cyber-input-wrapper:focus-within::after {
  opacity: 1;
  box-shadow: 0 0 10px rgba(0, 243, 255, 0.5);
}

.cyber-custom-input {
  height: 50px;
  background: rgba(10, 10, 25, 0.7) !important;
  border: 1px solid rgba(0, 243, 255, 0.3) !important;
  border-radius: 8px !important;
  color: #e0e0ff !important;
  transition: all 0.3s ease;
}

.cyber-custom-input:focus {
  border-color: #00f3ff !important;
  box-shadow: 0 0 10px rgba(0, 243, 255, 0.5) !important;
}

:deep(.cyber-custom-input .el-input__wrapper) {
  background: transparent !important;
  box-shadow: none !important;
}

:deep(.cyber-custom-input .el-input__inner) {
  background: transparent !important;
  color: #e0e0ff !important;
  caret-color: #00f3ff;
}

:deep(.cyber-custom-input .el-input__inner::placeholder) {
  color: rgba(224, 224, 255, 0.5) !important;
}

:deep(.cyber-custom-input .el-input__prefix) {
  color: #00f3ff;
}

.cyber-captcha-section, .cyber-verification-section {
  display: flex;
  gap: 12px;
  align-items: center;
}

.cyber-captcha-input, .cyber-verification-input {
  flex: 1;
}

.cyber-img-verCode {
  width: 120px;
  height: 50px;
  cursor: pointer;
  border-radius: 8px;
  border: 1px solid rgba(0, 243, 255, 0.5);
  box-shadow: 0 0 10px rgba(0, 243, 255, 0.3);
  transition: all 0.3s ease;
}

.cyber-img-verCode:hover {
  box-shadow: 0 0 15px rgba(0, 243, 255, 0.5);
  transform: scale(1.02);
}

.cyber-send-code-btn {
  width: 130px;
  height: 50px;
  white-space: normal;
  line-height: 1.3;
  background: rgba(50, 50, 80, 0.7) !important;
  border: 1px solid rgba(0, 243, 255, 0.5) !important;
  color: #00f3ff !important;
  border-radius: 8px !important;
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
}

.cyber-send-code-btn:hover {
  background: rgba(0, 243, 255, 0.2) !important;
  box-shadow: 0 0 15px rgba(0, 243, 255, 0.4) !important;
  border-color: #00f3ff !important;
}

.cyber-send-code-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.cyber-btn-login {
  width: 100%;
  height: 50px;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 1px;
  border-radius: 10px;
  background: linear-gradient(90deg, #00f3ff, #00aaff, #00f3ff) !important;
  background-size: 200% auto !important;
  border: 1px solid #00f3ff !important;
  color: #0a0a1a !important;
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
  animation: gradient 3s linear infinite;
  animation-play-state: paused;
}

.cyber-btn-login:hover {
  animation-play-state: running;
  box-shadow: 0 0 20px rgba(0, 243, 255, 0.6);
  transform: translateY(-2px);
}

.btn-glow {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.4), transparent);
  transform: translateX(-100%);
  transition: transform 0.6s ease;
}

.cyber-btn-login:hover .btn-glow {
  transform: translateX(100%);
}

.btn-text {
  position: relative;
  z-index: 2;
}

.cyber-forget-password {
  text-align: center;
  margin-top: 10px;
}

.cyber-link {
  color: #00f3ff !important;
  font-size: 14px;
  font-weight: 500;
  text-shadow: 0 0 5px rgba(0, 243, 255, 0.5) !important;
  transition: all 0.3s ease;
}

.cyber-link:hover {
  color: #ff00ff !important;
  text-shadow: 0 0 10px rgba(255, 0, 255, 0.7) !important;
}

/* 动画效果 */
@keyframes scanline {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(100%);
  }
}

@keyframes pulse {
  0% {
    transform: scale(0.9);
    opacity: 0.7;
  }
  50% {
    transform: scale(1.1);
    opacity: 1;
  }
  100% {
    transform: scale(0.9);
    opacity: 0.7;
  }
}

@keyframes float {
  0%, 100% {
    transform: translate(0, 0);
  }
  25% {
    transform: translate(20px, 20px);
  }
  50% {
    transform: translate(0, 40px);
  }
  75% {
    transform: translate(-20px, 20px);
  }
}

@keyframes gradient {
  0% {
    background-position: 0% 50%;
  }
  100% {
    background-position: 200% 50%;
  }
}

/* 响应式设计 */
@media (max-width: 480px) {
  .cyber-login-box {
    padding: 10px;
    margin: 0 15px;
  }

  .cyber-form-login {
    padding: 30px 20px;
  }

  .cyber-title {
    font-size: 24px;
  }

  .cyber-captcha-section, .cyber-verification-section {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }

  .cyber-img-verCode, .cyber-send-code-btn {
    width: 100%;
  }
}
</style>
